package mysql

import (
	"novel/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	UserId  int64  `gorm:"type:int(11);not null"`
	NovelId int64  `gorm:"type:int(11);not null"`
	Content string `gorm:"type:varchar(255);not null"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func CommentShow(db *gorm.DB, in *novelrpc.CommentShowRequest) []*novelrpc.CommentShow {
	var comment []*novelrpc.CommentShow
	/*
	   SELECT
	     novels.*,
	     users.id,
	     users.head_image,
	     users.nick_name,
	     comments.content
	   FROM
	     comments
	     LEFT JOIN novels ON comments.novel_id = novels.id
	     LEFT JOIN users ON comments.user_id = users.id
	*/
	db.Debug().Model(&Comment{}).Select(" novels.*, users.id, users.head_image, users.nick_name, comments.content as comment_content").
		Joins("  LEFT JOIN novels ON comments.novel_id = novels.id").
		Joins(" LEFT JOIN users ON comments.user_id = users.id").
		Where("novels.id=?", in.NovelId).
		Order("comments.created_at desc").
		Find(&comment)
	return comment
}
